package controllers;

import java.util.ArrayList;

import models.Horse;
import models.News;
import play.data.validation.Required;
import play.mvc.Controller;
import play.mvc.With;

@With(Secure.class)
public class Newss extends Controller {
	public static void AddNews(@Required String Title, String Body)

	{
		if (!validation.hasErrors()) {
			ArrayList<News> listnews = (ArrayList) News.findAll();
			if (listnews.size() != 0) {

				for (int i = 0; i < listnews.size(); i++) {
					if (listnews.get(i).Title.equalsIgnoreCase(Title)) {
						flash.error("oops, there is already a news with the same title. please change it");
						render();
					}
				}
			}
			News news = new News(Title, Body);
			news.save();
			CreatedNews(news);

		} else {
			render();
		}

	}

	public static void GetId(News news) {
		System.out.println("ID IS " + news.Title);

		ViewNews(news.id);
	}

	public static void ViewNews(long id) {
		News news = News.findById(id);
		render(news);
	}

	public static void CreatedNews(News news) {
		render(news);
	}

	public static void ViewAllNews() {
		ArrayList newslist = (ArrayList) News.findAll();
		render(newslist);
	}

	public static void DeleteNews(long id) {
		News x = News.findById(id);
		x.delete();
		ViewAllNews();

	}

	public static void EditNews(long id) {
		News x = News.findById(id);
		render(x, id);

	}

	public static void Edit(long id, String Title, String Body) {

		if (Title != null) {
			News x = News.findById(id);
			x.Title = Title;
			x.Body = Body;
			x.save();
			ViewNews(id);
		} else {
			EditNews(id);
		}
	}

}
